//
//  HomeTableViewController.swift
//  新浪微博
//
//  Created by test on 15/7/27.
//  Copyright © 2015年 test. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {
    var statuses:[Status]? {
        didSet {
        
        tableView.reloadData()
        
        }

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !UserAccount.userLogon {
        visitorView?.setupViewInfo(true, imageName: "visitordiscover_feed_image_smallicon", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        loadData()
        prepareTableView()
    }

    func loadData() {
     Status.loadStatus {[weak self] (dataList, error) -> () in
        if error != nil {
          print(error)
            return
        
        }
        
        self?.statuses = dataList
        }
        
    }
    func prepareTableView() {
        tableView.registerClass(StatusForwardCell.self, forCellReuseIdentifier: StatusCellIdentifier.ForwardCell.rawValue)
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: StatusCellIdentifier.NormalCell.rawValue)
        tableView.estimatedRowHeight = 300
//        tableView.rowHeight = 300
//        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    }
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 根据微博类型，选取不同的 ID
        let status = statuses![indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier(StatusCellIdentifier.cellId(status), forIndexPath: indexPath) as! StatusCell
        cell.status = statuses![indexPath.row]
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let status = statuses![indexPath.row]
        if let h = status.rowHeight {
            return h
            
        }
        let cell = tableView.dequeueReusableCellWithIdentifier(StatusCellIdentifier.cellId(status)) as? StatusCell
        
        status.rowHeight = cell!.rowHeight(status)
        return status.rowHeight!
        
    }
}
